package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.nhc.model.CoordParmFileHandle;
import com.nhc.model.CoordSelfInfo;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.TopMenuWindow;

public class CoordFormatSelfActivity extends BaseActivity implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "CoordFormatSelfActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    static final String INTENT_COORDPARM_INFO = "INTENT_COORDPARM_INFO";

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */

    public static Intent createIntent(Context context) {
        return new Intent(context, CoordFormatSelfActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    CoordSelfInfo mSelfInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coordformatself_activity);

        mSelfInfo = CoordParmFileHandle.getInstance().mCoordParmInfoEdit;

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    private ScrollView svDemoMain;

    private TextView tv_ZLAT;
    private TextView tv_ZLON;
    private TextView tv_TY;
    private TextView tv_EW;
    private TextView tv_SN;


    @Override
    public void initView() {//必须调用
        svDemoMain = (ScrollView) findViewById(R.id.svDemoMain);

        tv_ZLAT = (TextView) findViewById(R.id.tv_ZLAT);
        tv_ZLON = (TextView) findViewById(R.id.tv_ZLON);
        tv_TY = (TextView) findViewById(R.id.tv_TY);
        tv_EW = (TextView) findViewById(R.id.tv_EW);
        tv_SN = (TextView) findViewById(R.id.tv_SN);

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    @Override
    public void initData() {//必须调用
        refresh();
    }

    void refresh() {
        tv_ZLAT.setText(mSelfInfo.ZLAT + "");
        tv_ZLON.setText(mSelfInfo.ZLON + "");
        tv_TY.setText(mSelfInfo.TY + "");
        tv_EW.setText(mSelfInfo.EW + "");
        tv_SN.setText(mSelfInfo.SN + "");

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.coordself_0).setOnClickListener(this);
        findViewById(R.id.coordself_1).setOnClickListener(this);
        findViewById(R.id.coordself_2).setOnClickListener(this);
        findViewById(R.id.coordself_3).setOnClickListener(this);
        findViewById(R.id.coordself_4).setOnClickListener(this);
    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void finish() {
        CoordParmFileHandle.getInstance().save(this);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onForwardClick(View v) {
        //top栏右边按钮响应
        toActivity(TopMenuWindow.createIntent(context, getResources().getStringArray(R.array.top_menu)), REQUEST_TO_TOP_MENU, false);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.coordself_0:
                editParm(0);
                break;
            case R.id.coordself_1:
                editParm(1);
                break;
            case R.id.coordself_2:
                editParm(2);
                break;
            case R.id.coordself_3:
                editParm(3);
                break;
            case R.id.coordself_4:
                editParm(4);
                break;
            default:
                break;
        }
    }

    private void editParm(int pos) {

        String strT = getString(R.string.coord_zylat);
        int REQUEST = REQUEST_TO_ZLAT;
        double value = mSelfInfo.ZLAT;
        switch (pos) {
            default:
            case 0:
                intent = EditNumberActivity.createIntentDLimt(context
                        , strT + getString(R.string.parmsetting), value, "", -90, 90);
                break;
            case 1:
                strT = getString(R.string.coord_zylon);
                value = mSelfInfo.ZLON;
                REQUEST = REQUEST_TO_ZLON;
                intent = EditNumberActivity.createIntentDLimt(context
                        , strT + getString(R.string.parmsetting), value, "", -180, 180);
                break;
            case 2:
                strT = getString(R.string.coord_tyscale);
                value = mSelfInfo.TY;
                REQUEST = REQUEST_TO_TY;
                intent = EditNumberActivity.createIntentD(context
                        , strT + getString(R.string.parmsetting), value, "");
                break;
            case 3:
                strT = getString(R.string.coord_dxpc);
                value = mSelfInfo.EW;
                REQUEST = REQUEST_TO_EW;
                intent = EditNumberActivity.createIntent(context
                        , strT + getString(R.string.parmsetting), (int) value, "");
                break;
            case 4:
                strT = getString(R.string.coord_nbpc);
                value = mSelfInfo.SN;
                REQUEST = REQUEST_TO_SN;
                intent = EditNumberActivity.createIntent(context
                        , strT + getString(R.string.parmsetting), (int) value, "");
                break;
        }

        toActivity(intent, REQUEST);
    }


    private final int REQUEST_TO_TOP_MENU = 30;
    private final int REQUEST_TO_ZLAT = 31;
    private final int REQUEST_TO_ZLON = 32;
    private final int REQUEST_TO_TY = 33;
    private final int REQUEST_TO_EW = 34;
    private final int REQUEST_TO_SN = 35;


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_ZLAT:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.ZLAT = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_ZLON:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.ZLON = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_TY:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.TY = Double.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_EW:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.EW = Integer.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_SN:
                if (data != null) {
                    svDemoMain.smoothScrollTo(0, 0);
                    String editedValue = data.getStringExtra(EditNumberActivity.RESULT_VALUE);
                    mSelfInfo.SN = Integer.valueOf(editedValue);
                    refresh();
                }
                break;
            case REQUEST_TO_TOP_MENU:
                if (data != null) {
                    switch (data.getIntExtra(TopMenuWindow.RESULT_POSITION, -1)) {
                        case 0:
                            mSelfInfo.reset();
                            refresh();
                            break;
                        default:
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {

    }
    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
